﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Kaixin
{
    using Kaixin.Tools;

    public class Loginout
    {
        //
        // this code is generated at client side by js
        // format: (random)_(now)
        //
        private static string rcode = "0.46268334910510445_1244712897446";

        // return value: 
        //  1: succ
        //  0: fail
        // -1: need code
        public static int Login(Network network, string user, string pass, out string uid)
        {
            uid = "";
            if (!user.Equals("") && !pass.Equals(""))
            {
                string data = "email=" + user + "&password=" + pass + "&remember=1" + "&url=/home/";
                string resp = network.PostData(Consts.RootURL+ Consts.LoginUrl, Encoding.ASCII.GetBytes(data));
                //string resp = network.CallUrl(Consts.RootURL + "/home");

                if (resp == null || "".Equals(resp))
                    resp = network.CallUrl(Consts.RootURL + "/home");

                if (resp != null && !"".Equals(resp))
                {
                    if (resp.IndexOf(Consts.LoginErrorInfo) < 0)
                    {
                        string pat = "<a href=\"/photo/logolist.php\\?uid=(\\d+?)\">";

                        Match m = Regex.Match(resp, pat);

                        if (m.Success)
                        {
                            uid = m.Groups[1].Value;
                        }
                        TextOutput.PrintLine("Login succ!");
                        return 1;
                    }
                    else if (CheckNeedCode(resp))
                    {
                        TextOutput.PrintLine("- Need Code -");
                        return -1;
                    }
                    else
                        return 0;
                }
                else
                    return 0;
            }
            else
                return 0;
        }

        public static string GetUid(Network network)
        {
            string uid = "";
            string url = Consts.RootURL + "/home";
            string resp = network.CallUrl(url);

            if (resp.IndexOf(Consts.LoginErrorInfo) < 0)
            {
                string pat = "<a href=\"/photo/logolist.php\\?uid=(\\d+?)\">";

                Match m = Regex.Match(resp, pat);

                if (m.Success)
                {
                    uid = m.Groups[1].Value;
                }
            }

            return uid;

        }

        // return value: 
        //  1: succ
        //  0: fail
        public static int LoginWithCode(Network network, string user, string pass, string code, out string uid)
        {
            uid = "";
            if (!user.Equals("") && !pass.Equals(""))
            {
                string data = "email=" + user + "&password=" + pass + "&remember=1" + "&url=/home/&invisible_mode=0&rpkey=";
                data += "&code=" + code + "&rcode=" + rcode;
                //data += "&code=xu4W&rcode=0.4382996575590906_1244790630752";
                string resp = network.PostData(Consts.RootURL + Consts.LoginUrl, Encoding.UTF8.GetBytes(data));

                if (resp == null || "".Equals(resp))
                    resp = network.CallUrl(Consts.RootURL + "/home");

                if (!resp.Equals("") && resp.IndexOf(Consts.LoginErrorInfo) < 0)
                {
                    string pat = "<a href=\"/photo/logolist.php\\?uid=(\\d+?)\">";

                    Match m = Regex.Match(resp, pat);

                    if (m.Success)
                    {
                        uid = m.Groups[1].Value;
                    }
                    TextOutput.PrintLine("Login succ!");
                    return 1;
                }
                else
                    return 0;
            }
            else
                return 0;
        }


        public static byte[] GetCode(Network network)
        {
            long t1 = new DateTime(1970, 1, 1).Ticks;
            long t2 = DateTime.Now.Ticks;

            Random r = new Random((int)t2);

            rcode = r.NextDouble().ToString() + "_" + ((t2 - t1) / 10000);

            string url = Consts.RootURL + Consts.LoginRcodeUrl +
                "?randnum=" + rcode + "&norect=1";

            byte[] data = network.GetData(url);
            return data;
        }

        public static bool CheckNeedCode(string resp)
        {
            if (resp != null && !resp.Equals(""))
            {
                //   "if (\"12\" == \"12\" || \"12\" == \"11\" || \"12\" == \"10\")"
                string pat = "if \\(\"(\\d+?)\" == \"12\" \\|\\| \"(\\d+?)\" == \"11\" \\|\\| \"(\\d+?)\" == \"10\"\\)";
                Match m = Regex.Match(resp, pat);
                if (m.Success)
                {
                    string v = m.Groups[1].Value;
                    if (v.Equals("10") || v.Equals("11") || v.Equals("12"))
                        return true;
                }
            }

            return false;
        }

        public static void Logout(Network network)
        {
            network.CallUrl(Consts.RootURL + Consts.LogoutUrl);
        }
    }
}
